package com.example.appp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class GameMap extends View {

    private static final int COUNT_HEIGHT_BLOCK = 24;
    private static final int COUNT_WIDTH_BLOCK = 16;
    private static final int WALL = 200;
    private static final int ROAD = 500;
    Bitmap bitmapBackground;
    Bitmap bitmapPoint;
    Bitmap bitmapMap;
    int step;
    int size;
    Rect rectangleMap = new Rect();
    int bufferX;
    int bufferY;

    int way;

    public int[][] map = { { 4, 2, 2, 2, 2, 2, 2, 5, 4, 2, 2, 2, 2, 2, 2, 5 },
            { 3, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 3 },
            { 3, 0, 9, 2, 2, 10, 0, 3, 3, 0, 9, 2, 2, 10, 0, 3 },
            { 3, 0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 3 },
            { 3, 0, 11, 0, 4, 10, 0, 0, 0, 0, 9, 5, 0, 11, 0, 3 },
            { 3, 0, 3, 0, 3, 0, 0, 4, 5, 0, 0, 3, 0, 3, 0, 3 },
            { 3, 0, 3, 0, 3, 0, 4, 6, 7, 5, 0, 3, 0, 3, 0, 3 },
            { 3, 0, 8, 0, 8, 0, 3, 0, 0, 3, 0, 8, 0, 8, 0, 3 },
            { 3, 0, 0, 0, 0, 0, 7, 2, 2, 6, 0, 0, 0, 0, 0, 3 },
            { 7, 5, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 4, 6 },
            { 2, 6, 0, 8, 0, 4, 10, 0, 0, 9, 5, 0, 8, 0, 7, 2 },
            { 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
            { 12, 10, 0, 11, 0, 7, 2, 2, 2, 2, 6, 0, 11, 0, 9, 12 },
            { 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3 },
            { 3, 0, 4, 6, 0, 9, 5, 0, 0, 4, 10, 0, 7, 5, 0, 3 },
            { 3, 0, 8, 0, 0, 0, 13, 10, 0, 3, 0, 0, 0, 8, 0, 3 },
            { 3, 0, 0, 0, 11, 0, 8, 0, 0, 8, 0, 11, 0, 0, 0, 3 },
            { 13, 10, 0, 9, 6, 0, 0, 0, 0, 0, 0, 7, 10, 0, 9, 14 },
            { 3, 0, 0, 0, 0, 0, 9, 12, 12, 10, 0, 0, 0, 0, 0, 3 },
            { 3, 0, 9, 10, 0, 0, 0, 3, 3, 0, 0, 0, 9, 10, 0, 3 },
            { 3, 0, 0, 0, 0, 11, 0, 3, 3, 0, 11, 0, 0, 0, 0, 3 },
            { 3, 0, 9, 2, 2, 6, 0, 7, 6, 0, 7, 2, 2, 10, 0, 3 },
            { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
            { 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6 }, };
    public int[][] inverseMap = new int[COUNT_HEIGHT_BLOCK][COUNT_HEIGHT_BLOCK];
    public int[][] secondInverseMap = new int[COUNT_HEIGHT_BLOCK][COUNT_HEIGHT_BLOCK];
    int width;

    public GameMap(Context context, int width) {
        super(context);
         bitmapBackground = BitmapFactory.decodeResource(getResources(),
         R.drawable.background);
         bitmapPoint = BitmapFactory.decodeResource(getResources(),
         R.drawable.ppoint);
         bitmapMap = BitmapFactory.decodeResource(getResources(),
         R.drawable.map_1);
        way = 4;

        size = (width / 16);// iaiyou

    }

    public void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmapMap, 0, 0, new Paint());
        for (int column = 0; column < COUNT_WIDTH_BLOCK; column++) {
            for (int row = 0; row < COUNT_HEIGHT_BLOCK; row++) {
                if (map[row][column] == 1) {
                    canvas.drawBitmap(bitmapPoint, column * size, row * size,
                            new Paint());
                }
            }
        }
    }

    void pointMap() {
        for (int column = 0; column < COUNT_WIDTH_BLOCK; column++) {
            for (int row = 0; row < COUNT_HEIGHT_BLOCK; row++) {
                if (map[row][column] == 0) {
                    map[row][column] = 1;
                }
            }
        }

    }

    void onDrawGrid(Canvas canvas) {
        Paint paintBlack = new Paint();
        paintBlack.setColor(Color.BLACK);
        Paint paintWhite = new Paint();
        paintWhite.setColor(Color.WHITE);
        int depth = 2;
        for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
            for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                canvas.drawRect(row * size, column * size, row * size + size, column * size
                        + size, paintBlack);
                canvas.drawRect((row * size) + depth, (column * size) + depth,
                        (row * size + size) - depth, (column * size + size) - depth, paintWhite);
                canvas.drawText(Integer.toString(inverseMap[row][column]), row * size
                        + depth, (column + 1) * size - depth, new Paint());
            }
        }
    }

    void interSecrRect(Rect rectPac) {
        for (int column = 0; column < COUNT_WIDTH_BLOCK; column++) {
            for (int row = 0; row < COUNT_HEIGHT_BLOCK; row++) {
                if (map[row][column] == 1) {
                    rectangleMap.set(column * size, row * size, column * size
                            + 10, row * size + 10);
                    if (rectPac.intersect(rectangleMap)) {
                        map[row][column] = 0;
                    }
                }
            }
        }
    }

    void inversMap() {
        for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
            for (int colunm = 0; colunm < COUNT_HEIGHT_BLOCK; colunm++) {
                if (map[colunm][row] == 0 || map[colunm][row] == 1) {
                    inverseMap[row][colunm] = 0;
                } else {
                    inverseMap[row][colunm] = WALL;
                }
            }
        }
    }

    public void findPath(int x, int y) {
        inversMap();
        int count = 0;
        step = 2;

        inverseMap[x / size][y / size] = 1;
        
        while (count < 50) {
            for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                    if (inverseMap[row][column] == step - 1) {
                        if (row > 1) {
                            if (inverseMap[row - 1][column] == 0) {
                                inverseMap[row - 1][column] = step;
                            }
                        }
                        if (row < 23) {
                            if (inverseMap[row + 1][column] == 0) {
                                inverseMap[row + 1][column] = step;
                            }
                        }
                        if (column > 1) {
                            if (inverseMap[row][column - 1] == 0) {
                                inverseMap[row][column - 1] = step;
                            }
                        }
                        if (column < 23) {
                            if (inverseMap[row][column + 1] == 0) {
                                inverseMap[row][column + 1] = step;
                            }
                        }
                    }
                }
            }
            step++;
            count++;
        }

    }

    public void findPath(boolean isRight, boolean isLeft, boolean isUp,
            boolean isDown, int x, int y) {

        for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
            for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                if (map[column][row] == 0 || map[column][row] == 1) {
                    secondInverseMap[row][column] = 0;
                } else
                    secondInverseMap[row][column] = WALL;
            }
        }

        int count = 0;
        step = 2;

        secondInverseMap[x / size][y / size] = 1;
        while (count < 50) {
            for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                    if (secondInverseMap[row][column] == step - 1) {
                        if (row > 1) {
                            if (secondInverseMap[row - 1][column] == 0) {
                                secondInverseMap[row - 1][column] = step;
                            }
                        }
                        if (row < COUNT_HEIGHT_BLOCK - 1) {
                            if (secondInverseMap[row + 1][column] == 0) {
                                secondInverseMap[row + 1][column] = step;
                            }
                        }
                        if (column > 1) {
                            if (secondInverseMap[row][column - 1] == 0) {
                                secondInverseMap[row][column - 1] = step;
                            }
                        }
                        if (column < COUNT_HEIGHT_BLOCK - 1) {
                            if (secondInverseMap[row][column + 1] == 0) {
                                secondInverseMap[row][column + 1] = step;
                            }
                        }
                    }
                }
            }
            step++;
            count++;
        }

        secondInverseMap[x / size][y / size] = ROAD;

        int back = 1;
        if (isLeft) {
            while (back < 5) {
                for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                    for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                        if (secondInverseMap[row][column] == ROAD) {
                            if (secondInverseMap[row - 1][column] == back + 1) {
                                secondInverseMap[row - 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row][column - 1] == back + 1) {
                                secondInverseMap[row][column - 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row][column + 1] == back + 1) {
                                secondInverseMap[row][column + 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row + 1][column] == back + 1) {
                                secondInverseMap[row + 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            }
                        }
                    }
                }
                back++;
            }

        }

        if (isRight) {
            while (back < 5) {
                for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                    for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                        if (secondInverseMap[row][column] == ROAD) {
                            if (secondInverseMap[row + 1][column] == back + 1) {
                                secondInverseMap[row + 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row][column - 1] == back + 1) {
                                secondInverseMap[row][column - 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row][column + 1] == back + 1) {
                                secondInverseMap[row][column + 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row - 1][column] == back + 1) {
                                secondInverseMap[row - 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            }
                        }
                    }
                }
                back++;
            }
        }

        if (isDown) {
            while (back < 5) {
                for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                    for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                        if (secondInverseMap[row][column] == ROAD) {
                            if (secondInverseMap[row][column + 1] == back + 1) {
                                secondInverseMap[row][column + 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row + 1][column] == back + 1) {
                                secondInverseMap[row + 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row - 1][column] == back + 1) {
                                secondInverseMap[row - 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row][column - 1] == back + 1) {
                                secondInverseMap[row][column - 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            }

                        }
                    }
                }
                back++;
            }
        }

        if (isUp) {
            while (back < 5) {
                for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
                    for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                        if (secondInverseMap[row][column] == ROAD) {
                            if (secondInverseMap[row][column - 1] == back + 1) {
                                secondInverseMap[row][column - 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row - 1][column] == back + 1) {
                                secondInverseMap[row - 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row + 1][column] == back + 1) {
                                secondInverseMap[row + 1][column] = ROAD;
                                secondInverseMap[row][column] = 0;
                            } else if (secondInverseMap[row][column + 1] == back + 1) {
                                secondInverseMap[row][column + 1] = ROAD;
                                secondInverseMap[row][column] = 0;
                            }
                        }
                    }
                }
                back++;
            }
        }

        for (int row = 0; row < COUNT_WIDTH_BLOCK; row++) {
            for (int column = 0; column < COUNT_HEIGHT_BLOCK; column++) {
                if (secondInverseMap[row][column] == ROAD) {
                    bufferX = row;
                    bufferY = column;
                }
            }
        }

        findPath(bufferX * size, bufferY * size);

    }
}